(library (lib mime-types)
  (export guess-mime-type
          guess-binary-file?)
  (import
    (except (rnrs base) let-values)
    (only (guile) lambda* λ error when display sleep)
    ;; SRFIs
    ;; hash tables
    (prefix (srfi srfi-69) srfi-69:)
    ;; strings
    (prefix (srfi srfi-13) srfi-13:)
    (prefix (fslib) fslib:)
    (prefix (logging) log:)))


(define guess-mime-type
  (λ (file-location)
    (let ([file-ext (fslib:file-extension file-location)])
      (srfi-69:hash-table-ref file-extension-mime-types
                              file-ext
                              ;; default guess is plain text
                              (λ () 'text/plain)))))


(define guess-binary-file?
  (λ (file-location)
    (let ([file-ext (fslib:file-extension file-location)])
      (srfi-69:hash-table-ref file-extension-binary
                              file-ext
                              ;; default guess is that the file is a not binary file
                              (λ () #f)))))


;; The following hash-table contains MIME types for common file types.
;; See also:
;; https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
;; https://www.iana.org/assignments/media-types/media-types.xhtml

;; This list of MIME types is not comprehensive. It is only a personal opinion
;; of what might be needed.
(define file-extension-mime-types
  (srfi-69:alist->hash-table
   '(("bz" . application/x-bzip)
     ("bz2" . application/x-bzip2)
     ("css" . text/css)
     ("csv" . text/csv)
     ("gz" . application/gzip)
     ("gif" . image/gif)
     ("htm" . text/html)
     ("html" . text/html)
     ("jpg" . image/jpeg)
     ("jpeg" . image/jpeg)
     ("js" . text/javascript)
     ("json" . application/json)
     ("mjs" . text/javascript)
     ("odp" . application/vnd.oasis.opendocument.presentation)
     ("ods" . application/vnd.oasis.opendocument.spreadsheet)
     ("odt" . application/vnd.oasis.opendocument.text)
     ("oga" . audio/ogg)
     ("ogx" . application/ogg)
     ("png" . image/png)
     ("pdf" . application/pdf)
     ("sh" . application/x-sh)
     ("svg" . image/svg+xml)
     ("tar" . application/x-tar)
     ("tif" . image/tiff)
     ("tiff" . image/tiff)
     ("ttf" . font/ttf)
     ("txt" . text/plain)
     ("wav" . audio/wav)
     ("weba" . audio/webm)
     ("webm" . video/webm)
     ("webp" . image/webp)
     ("xhtml" . application/xhtml+xml)
     ("xul" . application/vnd.mozilla.xul+xml)
     ("zip" . application/zip)
     ("7z" . application/x-7z-compressed))))


;; The following hash-table contains MIME types for common file types.
;; See also:
;; https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
;; https://www.iana.org/assignments/media-types/media-types.xhtml

;; This list of MIME types is not comprehensive. It is only a personal opinion
;; of what might be needed.
(define file-extension-binary
  (srfi-69:alist->hash-table
   '(("bz" . #t)
     ("bz2" . #t)
     ("css" . #f)
     ("csv" . #f)
     ("gz" . #t)
     ("gif" . #t)
     ("htm" . #f)
     ("html" . #f)
     ("jpg" . #t)
     ("jpeg" . #t)
     ("js" . #f)
     ("json" . #f)
     ("mjs" . #f)
     ("odp" . #t)
     ("ods" . #t)
     ("odt" . #t)
     ("oga" . #t)
     ("ogx" . #t)
     ("png" . #t)
     ("pdf" . #t)
     ("sh" . #f)
     ("svg" . #f)  ; should svg be considered binary?
     ("tar" . #t)
     ("tif" . #t)
     ("tiff" . #t)
     ("ttf" . #t)
     ("txt" . #f)
     ("wav" . #t)
     ("weba" . #t)
     ("webm" . #t)
     ("webp" . #t)
     ("xhtml" . #f)
     ("xul" . #f)
     ("zip" . #t)
     ("7z" . #t))))
